﻿using JsonConversion;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using RTracker.Models.Common;
using RTracker.Models.ErrorLog;
using System.Web.Security;

namespace RTracker.Areas.Recruitment.Models.Calling
{
    public class MCalling
    {
        /// static variable RowSize from config settings to get list of this row size.
        public static int nRowSize = Convert.ToInt32(ConfigurationManager.AppSettings["TotalRecordsInListCalling"]);

        /// <summary>
        /// This method is used to create a user in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCalling">This object contains details of the user.</param>
        /// <returns>CStatus which is an object that contain transaction details</returns>
        public CStatus CreateCalling(CAuthenticationContext objCAuthenticationContext, ref CCalling objCCalling)
        {
            CStatus oCStatus = new CStatus();
           

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_create_calling_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@calling_id", SqlDbType.BigInt).Value = objCCalling.CallingId;
                    cmd.Parameters.Add("@calling_desc", SqlDbType.NVarChar).Value = objCCalling.CallingDesc;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCCalling.OrganizationId;
                    cmd.Parameters.Add("@client", SqlDbType.Int).Value = objCCalling.Client;
                    cmd.Parameters.Add("@department", SqlDbType.Int).Value = objCCalling.Department;
                    cmd.Parameters.Add("@vacancy", SqlDbType.Int).Value = objCCalling.Vacancy;
                    cmd.Parameters.Add("@candidate", SqlDbType.Int).Value = objCCalling.Candidate;
                    cmd.Parameters.Add("@call_timestamp", SqlDbType.DateTime).Value = objCCalling.CallTimeStamp;
                    cmd.Parameters.Add("@call_status", SqlDbType.Int).Value = objCCalling.CallStatus;
                   

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// This method is used to delete a user in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCalling">This object contains details of the user.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus DeleteCalling(CAuthenticationContext objCAuthenticationContext, ref CCalling objCCalling)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_delete_calling_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;
                    cmd.Parameters.Add("@calling_ids", SqlDbType.NVarChar).Value = objCCalling.CallingIds;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// This method is used to update the calling detail in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCalling">This object contains details of the user.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus UpdateCalling(CAuthenticationContext objCAuthenticationContext, ref CCalling objCCalling)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_update_calling_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;


                    cmd.Parameters.Add("@calling_id", SqlDbType.BigInt).Value = objCCalling.CallingId;
                    cmd.Parameters.Add("@calling_desc", SqlDbType.NVarChar).Value = objCCalling.CallingDesc;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCCalling.OrganizationId;
                    cmd.Parameters.Add("@client", SqlDbType.Int).Value = objCCalling.Client;
                    cmd.Parameters.Add("@department", SqlDbType.Int).Value = objCCalling.Department;
                    cmd.Parameters.Add("@vacancy", SqlDbType.Int).Value = objCCalling.Vacancy;
                    cmd.Parameters.Add("@candidate", SqlDbType.Int).Value = objCCalling.Candidate;
                    cmd.Parameters.Add("@call_timestamp", SqlDbType.DateTime).Value = objCCalling.CallTimeStamp;
                    cmd.Parameters.Add("@call_status", SqlDbType.Int).Value = objCCalling.CallStatus;
                    //cmd.Parameters.Add("@logon_password", SqlDbType.NVarChar).Value = objCCalling.LogonPassword;
                                        //cmd.Parameters.Add("@last_successful_logout_timestamp", SqlDbType.DateTime).Value = objCCalling.LastSuccessfulLogoutTimestamp;
                    //cmd.Parameters.Add("@logon_failure_count", SqlDbType.Int).Value = objCCalling.LogonFailureCount;
                    //cmd.Parameters.Add("@is_system_generated", SqlDbType.Bit).Value = objCCalling.IsSystemGenerated;
                    

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }

            return oCStatus;
        }

        /// <summary>
        /// This method is used to get the calling detail in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCalling">This object contains details of the user.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus GetCallingDetail(CAuthenticationContext objCAuthenticationContext, ref CCalling objCCalling)
        {
            CStatus oCStatus = new CStatus();
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_calling_detail_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@calling_id", SqlDbType.Int).Value = objCCalling.CallingId;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;

                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    if (rowsAffected > 0)
                    {
                        objCCalling = dt.ConvertToListFromDataTableCalling()[0];
                    }

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.DetailSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Sets the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCalling">This object contains details of the calling.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetCallingSoftLock(CAuthenticationContext objCAuthenticationContext, CCalling objCCalling)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_set_calling_soft_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@calling_id", SqlDbType.NVarChar).Value = objCCalling.CallingId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// Sets the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCalling">This object contains details of the user.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetCallingHardLock(CAuthenticationContext objCAuthenticationContext, CCalling objCCalling)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_set_calling_hard_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@calling_id", SqlDbType.NVarChar).Value = objCCalling.CallingId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// Releases the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCalling">This object contains details of the user.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseCallingSoftLock(CAuthenticationContext objCAuthenticationContext, CCalling objCCalling)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_release_calling_soft_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@calling_id", SqlDbType.NVarChar).Value = objCCalling.CallingId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// Releases the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCalling">This object contains details of the user.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseCallingHardLock(CAuthenticationContext objCAuthenticationContext, CCalling objCCalling)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_release_calling_hard_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@calling_id", SqlDbType.NVarChar).Value = objCCalling.CallingId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        ///  This function is used to get complete Calling list
        /// </summary>
        /// <returns>List of type CCalling</returns>
        public GCalling GetCallingGridList(CCalling objCCalling, CAuthenticationContext objCAuthenticationContext)
        {
            GCalling objGCalling = new GCalling();
            int nTotalRecords = 0;
            int nCurrentRowSize = nRowSize;

            if (objCCalling.RowSize != 0)
            {
                nCurrentRowSize = objCCalling.RowSize;
            }
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_calling_paging_list_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@calling_id", SqlDbType.Int).Value = objCCalling.CallingId;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCCalling.OrganizationId;
                    cmd.Parameters.Add("@page_number", SqlDbType.Int).Value = objCCalling.PageNumber;
                    cmd.Parameters.Add("@row_size", SqlDbType.Int).Value = nCurrentRowSize;
                    cmd.Parameters.Add("@sort_on", SqlDbType.VarChar).Value = objCCalling.SortOn;
                    cmd.Parameters.Add("@sort_type", SqlDbType.VarChar).Value = objCCalling.SortType;
                    cmd.Parameters.Add("@search", SqlDbType.VarChar).Value = objCCalling.Search;
                    cmd.Parameters.Add("@searchon", SqlDbType.VarChar).Value = objCCalling.SearchOn;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    objGCalling.Paging = new CPaging();
                    if (rowsAffected > 0)
                    {
                        objGCalling.LstCalling = dt.ConvertToListFromDataTableCalling();
                        objGCalling.LstCalling[0].PageNumber = int.Parse(cmd.Parameters["@idOut"].Value.ToString());
                        objGCalling.Paging.PageNumber = objGCalling.LstCalling[0].PageNumber;
                    }
                    else
                    {
                        objGCalling.LstCalling = null;
                    }


                    nTotalRecords = int.Parse(cmd.Parameters["@error"].Value.ToString());

                    objGCalling.Status = new CStatus();

                    if (nTotalRecords < 0)
                    {
                        objGCalling.Status.Success = false;
                        objGCalling.Status.StatusDetails = nTotalRecords.DbError();
                    }
                    else
                    {
                        objGCalling.Paging.TotalRecordsCount = nTotalRecords;
                        objGCalling.Paging.PageSize = nCurrentRowSize;

                        objGCalling.Status.Success = true;
                        objGCalling.Status.StatusDetails = "success";
                    }



                }
                catch (Exception ex)
                {
                    objGCalling.Status = new CStatus();
                    objGCalling.Status.Success = false;
                    objGCalling.Status.StatusDetails = ex.Message;
                }
            }
            return objGCalling;
        }

        

    }
}